package com.hq.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hq.mapper.AdminMapper;
import com.hq.pojo.Appointment;
import com.hq.mapper.RoleLoginMapper;
import com.hq.pojo.Doctor;
import com.hq.pojo.RoleLogin;
import com.hq.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private RoleLoginMapper roleLoginMapper;
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public PageInfo<Appointment> selectAllUser(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        // 查询数据库，获取查询数据
        List<Appointment> userList = adminMapper.selectAllUser();
        // 封装查询的数据在pageInfo对象中
        return new PageInfo<>(userList);
    }

    @Override
    public Map<String, String> updatePass(String name, String pwd, String newPass, String renewPass) {
        RoleLogin roleLogin = roleLoginMapper.roleLogin (new RoleLogin (name, pwd));
        Map<String, String> map = new HashMap<>();

        if (pwd == roleLogin.getPassword ()) {
            map.put ("updateInfo", "原密码错误");
            map.put ("code", "201");
            return map;
        } else {
            if (newPass.equals (renewPass)) {
                map.put ("code", "200");
                adminMapper.updatePassword (new RoleLogin (name, newPass));
                return map;
            } else {
                map.put ("updateInfo", "密码输入不一致");
                return map;
            }
        }
    }

    @Override
    public PageInfo<Doctor> selectAllDoctor(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        // 查询数据库，获取查询数据
        List<Doctor> doctorList = adminMapper.selectAllDoctor ();
        // 封装查询的数据在pageInfo对象中
        return new PageInfo<>(doctorList);
    }

    @Override
    public PageInfo<Appointment> selectDoctorAllUser(String name) {
        // 查询数据库，获取查询数据
        List<Appointment> appointList = adminMapper.selectDoctorAllUser (name);
        // 封装查询的数据在pageInfo对象中
        return new PageInfo<>(appointList);
    }

    //删除Appoint
    @Override
    public void delAppoint(Integer appointId) {
        adminMapper.deleteAppointment(appointId);
    }

    //删除doctor
    @Override
    public void delDoctor(Integer doctorId) {
        adminMapper.deleteDoctor(doctorId);
    }
}
